<%@ page import="com.eyas.business.model.jpa.AdminInfo" %>
<%@ page import="com.eyas.business.config.webmvc.interceptor.LoginInterceptor" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta charset="utf-8">
    <title>开班管理</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="bookmark" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/css/layui.css" media="all">
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/layui/layui.js"
            charset="utf-8"></script>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/private/css/simplecalendar/simple-calendar.css">

    <script type="text/javascript"
            src="${pageContext.request.contextPath}/private/js/simplecalendar/simple-calendar.js"></script>
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/jquery/jquery.js"></script>
    <script type="application/javascript"
            src="${pageContext.request.contextPath}/private/js/loading/ajaxloading.js"></script>
</head>
<body>
<fieldset class="layui-elem-field">
    <legend>日历</legend>
    <div id='container'></div>
</fieldset>
<fieldset class="layui-elem-field">
    <legend>查询结果</legend>
    <div>
        <table class="layui-table" lay-filter="gridfilter"
               lay-data="{url:'',page:false, id:'gridresult',height:'330px',width:'1080',toolbar:'#toolbarDemo',defaultToolbar: ['filter']}">
            <thead>
            <tr>
                <th lay-data="{field:'recordid',hide:true}">开班id</th>
                <th lay-data="{field:'classid',hide:true}">课程id</th>
                <th lay-data="{field:'coachid',hide:true}">教练id</th>
                <th lay-data="{field:'status',hide:true}">课程状态编码</th>
                <th lay-data="{field:'classname',align:'center',width:'120'}">课程名称</th>
                <th lay-data="{field:'agerange',align:'center',width:'120'}">适龄年龄段</th>
                <th lay-data="{field:'coachname',align:'center',width:'120'}">上课教练</th>
                <th lay-data="{field:'assistantsname',align:'center',width:'200'}">助教姓名</th>
                <th lay-data="{field:'address',align:'center',width:'150'}">上课地点</th>
                <th lay-data="{field:'starttime',align:'center',width:'120'}">开课日期</th>
                <th lay-data="{field:'begintime',align:'center',width:'120'}">上课时间</th>
                <th lay-data="{field:'endtime',align:'center',width:'120'}">下课时间</th>

                <th lay-data="{field:'maxnum',align:'center',width:'120'}">最大上课人数</th>
                <th lay-data="{field:'currnum',align:'center',width:'120'}">报名人数</th>
                <th lay-data="{field:'statustext',align:'center',width:'120'}">课程状态</th>
                <th lay-data="{fixed: 'right',align:'center', toolbar: '#barDemo',width:'150'}">操作</th>
            </tr>
            </thead>
        </table>
    </div>
</fieldset>
</body>
<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addRecordInfo">开班备课</button>
    </div>
</script>
<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <% if(((AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME)).getAdminnid()==1) { %>
    <a class="layui-btn layui-btn-xs" lay-event="remove">删除记录</a>
    <% } %>
</script>
<script type="application/javascript">
    var selected_day$;
    var selected_month$;
    var selected_year$;
    var table;
    var myCalendar;
    layui.use('table', function () {
        //数据表格模块
        table = layui.table;
        table.reload('gridresult', {
            url: '${pageContext.request.contextPath}/eyas/recordinfo/queryRecordInfoForPage'
            , where: {} //设定异步数据接口的额外参数
            , page: {
                curr: 1
            },
            method: 'get'
        });

        //监听行工具事件
        table.on('tool(gridfilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var win_1 = layer.open({
                    type: 2,
                    title: '班级信息编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/recordinfo/edit?recordid=' + data.recordid,
                    end: function () {
                        var starttime = data.starttime.replace(/-/g,"/");
                        var date = new Date(starttime);
                        var selecttime_ = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
                        if($.inArray(selecttime_,maskarray)>=0) {
                            maskarray.splice(maskarray.indexOf(selecttime_),1);
                        }
                        sendajax();
                        myCalendar.updateMark(selected_year$, selected_month$);
                        fn_addMask(selected_year$, selected_month$);
                    }
                });
                layer.full(win_1);
            } else if(obj.event === 'remove') {
                if(data.status!='4') {
                    layer.msg("只能删除[课程取消]状态的课程");
                    return;
                }

                $.ajax({
                    type : "DELETE",
                    url : "${pageContext.request.contextPath}/eyas/admin/superadmin/recordinfo/recordid/"+data.recordid,
                    dataType : "json",
                    success: function(xdata) {
                        var starttime = data.starttime.replace(/-/g,"/");
                        var date = new Date(starttime);
                        var selecttime_ = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
                        maskarray.splice(maskarray.indexOf(selecttime_),1);
                        sendajax();
                        myCalendar.updateMark(selected_year$, selected_month$);
                        fn_addMask(selected_year$, selected_month$);
                    },
                    error:function(xdata){
                        layer.msg(xdata.responseJSON.errormsg);
                    }
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(gridfilter)', function (obj) {
            if (obj.event === 'addRecordInfo') {
                var win_1 = layer.open({
                    type: 2,
                    title: '班级信息编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/recordinfo/edit',
                    end: function () {
                        sendajax();
                        fn_addMask(selected_year$, selected_month$);
                    }
                });
                layer.full(win_1);
            }
        });
        init_simple();
    });

    //改变日历的年份和月份时
    function dosom(year, month) {
        selected_year$ = year;
        selected_month$ = month;
        fn_addMask(selected_year$, selected_month$);
    }
    var maskarray = new Array();
    function fn_addMask(year, month) {
        //simplecalender addMark
        $.ajax({
            url: "${pageContext.request.contextPath}/eyas/recordinfo/Distinctstarttime",
            data: {year: year, month: month},
            type: "GET",
            dataType: "json",
            success: function (result) {
                var list = result;    //返回的数据
                if (list && list.length > 0) {
                    for (var i = 0; i < list.length; i++) {
                        if($.inArray(list[i].marktime,maskarray)<0) {
                            maskarray.push(list[i].marktime);
                            //myCalendar.addMark(list[i].marktime, '有课');
                            myCalendar.updateMark(selected_year$, selected_month$);
                        }
                    }
                }

            },
            error: function (xdata) {
                alert(xdata.responseJSON.errormsg);
            }
        });
    }

    function sendajax() {
        var selectdate = selected_year$ + "-" + selected_month$ + "-" + selected_day$;
        table.reload('gridresult', {
            url: '${pageContext.request.contextPath}/eyas/recordinfo/queryRecordInfoForPage'
            , where: {selectdate: selectdate} //设定异步数据接口的额外参数
            , page: {
                curr: 1
            },
            method: 'get'
        });
    }

    function init_simple() {
        var options = {
            width: '500px',
            height: '500px',
            language: 'CH', //语言
            showLunarCalendar: false, //阴历
            showHoliday: true, //休假
            showFestival: true, //节日
            showLunarFestival: false, //农历节日
            showSolarTerm: false, //节气
            showMark: true, //标记
            timeRange: {
                startYear: 1900,
                endYear: 2049
            },
            theme: {
                changeAble: false,
                weeks: {
                    backgroundColor: '#FBEC9C',
                    fontColor: '#4A4A4A',
                    fontSize: '20px',
                },
                days: {
                    backgroundColor: '#ffffff',
                    fontColor: '#565555',
                    fontSize: '24px'
                },
                todaycolor: 'orange',
                activeSelectColor: 'orange',
            }
        };
        myCalendar = new SimpleCalendar('#container', options);
        fn_addMask();
        sendajax();
    }

</script>
</html>
